package 行为型模式.备忘录;

/**
 * @ClassNameMemorandum
 * @Description
 * @Author
 * @Date2022/1/6 17:31
 **/
public class MemorandumTest {
    public static void main(String[] args) {
        Starter starter = new Starter();
        Manager manager = new Manager();
        starter.setState("S0");
        System.out.println("初始状态:" + starter.getState());
        manager.setMemento(starter.createMemorandum()); //保存状态
        starter.setState("S1");
        System.out.println("新的状态1:" + starter.getState());
        starter.setState("S2");
        System.out.println("新的状态2:" + starter.getState());
        starter.restoreMemento(manager.getMemento()); //恢复状态
        System.out.println("恢复状态:" + starter.getState());
    }
}

// 备忘录
class Memorandum{
    private String state;

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Memorandum(String state) {
        this.state = state;
    }
}
//发起人

class Starter {
    private String state;

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    Memorandum createMemorandum(){
        return new Memorandum(state);
    }

    public void restoreMemento(Memorandum m) {
        this.setState(m.getState());
    }
}

//管理者
class Manager{
    private Memorandum memento;
    public void setMemento(Memorandum m) {
        memento = m;
    }
    public Memorandum getMemento() {
        return memento;
    }
}
